package com.ndood;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * A.3.5 插入数据
 */
public class MongoDemo3 {

	public static void main(String[] args) {
		MongoCredential credential = MongoCredential.createCredential("root", "admin", "pay-boy-cyp".toCharArray());
		MongoClient client = new MongoClient(new ServerAddress("121.196.208.113", 27017), Arrays.asList(credential));
		MongoDatabase spitdb = client.getDatabase("spit");//打开数据库
		MongoCollection<Document> spit = spitdb.getCollection("spit");//获取集合

		Map<String,Object> map=new HashMap();
		map.put("content","我要吐槽");
		map.put("userid","9999");
		map.put("visits",123);
		map.put("publishtime",new Date());
		Document document=new Document(map);

		spit.insertOne(document);//插入数据
		client.close();

	}

}
